package com.hudongwx.sadpw.fragment.community;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hudongwx.sadpw.MyJsonObject;
import com.hudongwx.sadpw.R;
import com.hudongwx.sadpw.XListView;
import com.hudongwx.sadpw.activity.TieZiDetailActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * .
 */
@ContentView(R.layout.fragment_wlsd)
public class WLSDFragment extends Fragment implements XListView.IXListViewListener {
    String TAG = "WLSDFragment";
    JSONArray jsonArray;
    @ViewInject(R.id.wlsd_refresh_1)
    SwipeRefreshLayout swipeRefreshLayout;
    @ViewInject(R.id.wlsd_rec)
    private RecyclerView recyclerView;
    private View view;
    private MyWlsdAdapter myWlsdAdapter;
    private Handler handler = new Handler();
    private StaggeredGridLayoutManager staggeredGridLayoutManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null == view) {
            view = x.view().inject(this, inflater, container);
            staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(staggeredGridLayoutManager);
            myWlsdAdapter = new MyWlsdAdapter(jsonArray);
            recyclerView.setAdapter(myWlsdAdapter);
            parseWlsd();

        }
        return view;
    }

    //获得最后一个条目
    private int getMax(int[] i) {
        int max = 0;
        for (int i0 : i) {
            max = i0 > max ? i0 : max;
        }
        return max;
    }

    //解析json
    public void parseWlsd() {
        RequestParams params = new RequestParams("http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/Posts?boardId=5&pageSize=5&pageIndex=1");
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    Log.i(TAG, "onSuccess: ");
//                    myAdapter.updateItem(data);
                    myWlsdAdapter.updateItem(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onRefresh() {
        parseWlsd();
    }

    @Override
    public void onLoadMore() {

    }

    class MyWlsdAdapter extends RecyclerView.Adapter<MyWlsdViewHolder> {
        JSONArray jsonArray;

        public MyWlsdAdapter(JSONArray jsonArray) {
            this.jsonArray = jsonArray;
        }

        public void updateItem(JSONArray jsonArray) {
            this.jsonArray = jsonArray;
            notifyDataSetChanged();
        }

        @Override
        public MyWlsdViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.community_2, null);
            return new MyWlsdViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyWlsdViewHolder holder, final int position) {
            if (null == jsonArray) {
                recyclerView.setVisibility(View.GONE);
            } else {
                try {
                    x.image().bind(holder.imageView1, jsonArray.getJSONObject(position).getString("UserIcon"));
                    holder.textView1.setText(jsonArray.getJSONObject(position).getString("UserName"));
                    holder.textView2.setText(jsonArray.getJSONObject(position).getString("DateTime"));
                    if (jsonArray.getJSONObject(position).getString("CommentCount").equals("0")) {
                        holder.textView3.setText("回帖");
                    } else {
                        holder.textView3.setText(jsonArray.getJSONObject(position).getString("CommentCount"));
                    }
                    holder.textView4.setText(jsonArray.getJSONObject(position).getString("Title"));
                    final JSONArray array = jsonArray.getJSONObject(position).getJSONArray("ListFile");
                    if (array.length() == 1) {
                        x.image().bind(holder.imageView3, array.getJSONObject(0).getString("Url"));
                    } else if (array.length() == 2) {
                        x.image().bind(holder.imageView3, array.getJSONObject(0).getString("Url"));
                        x.image().bind(holder.imageView4, array.getJSONObject(1).getString("Url"));
                    } else if (array.length() >= 3) {
                        x.image().bind(holder.imageView3, array.getJSONObject(0).getString("Url"));
                        x.image().bind(holder.imageView4, array.getJSONObject(1).getString("Url"));
                        x.image().bind(holder.imageView5, array.getJSONObject(2).getString("Url"));
                    } else if (array.length() == 0) {
                        holder.ll.setVisibility(View.GONE);
                        holder.imageView3.setVisibility(View.GONE);
                        holder.imageView4.setVisibility(View.GONE);
                        holder.imageView5.setVisibility(View.GONE);
                    }
                    holder.ll1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(v.getContext(), TieZiDetailActivity.class);
                            try {
                                int id = jsonArray.getJSONObject(position).getInt("Id");
                                intent.putExtra("type", id);
                                getActivity().startActivity(intent);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                        @Override
                        public void onRefresh() {
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    swipeRefreshLayout.setRefreshing(false);
                                    Log.i(TAG, "run: 刷新完成");
                                }
                            }, 2000);
                            recyclerView.setAdapter(myWlsdAdapter);
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }

        @Override
        public int getItemCount() {
            if (null == jsonArray) return 0;
            return jsonArray.length();
        }
    }

    class MyWlsdViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView1, imageView2, imageView3, imageView4, imageView5;
        TextView textView1, textView2, textView3, textView4;
        LinearLayout ll, ll1;

        public MyWlsdViewHolder(View itemView) {
            super(itemView);
            imageView1 = (ImageView) itemView.findViewById(R.id.wlsd_iv_1);
            imageView2 = (ImageView) itemView.findViewById(R.id.wlsd_iv_2);
            textView1 = (TextView) itemView.findViewById(R.id.wlsd_tv_1);
            textView2 = (TextView) itemView.findViewById(R.id.wlsd_tv_2);
            textView3 = (TextView) itemView.findViewById(R.id.wlsd_tv_3);
            textView4 = (TextView) itemView.findViewById(R.id.wlsd_tv_4);
            imageView3 = (ImageView) itemView.findViewById(R.id.ll_iv_1);
            imageView4 = (ImageView) itemView.findViewById(R.id.ll_iv_2);
            imageView5 = (ImageView) itemView.findViewById(R.id.ll_iv_3);
            ll = (LinearLayout) itemView.findViewById(R.id.wlsd_ll);
            ll1 = (LinearLayout) itemView.findViewById(R.id.wlsd_ll_1);
        }
    }
}
